<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" v-auth="'ACTION_companyPlaceOrder'">下单</a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <CompanyOrderDrawer @register="registerCompanyOrderDrawer" @success="reload" />
    <CompanyUserGoodsListDrawer @register="registerCompanyUserGoodsListDrawer" />
  </div>
</template>
<script lang="ts" setup name="companyOrder">
import { ActionItem, BasicTable, TableAction } from '@apps/antd-admin/src/components/table';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';
import CompanyOrderDrawer from './components/CompanyOrderDrawer.vue';
import CompanyUserGoodsListDrawer from './components/CompanyUserGoodsListDrawer.vue';
import { useBaseTable } from '@apps/antd-admin/src/hooks';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';
import { columns, searchFormSchema } from './data/data';
import { Api, companyOrderRefund } from './data/api';

const { createMessage, createConfirm } = useMessage();
const [registerCompanyOrderDrawer, { openDrawer: openCompanyOrderDrawer }] = useDrawer();
const [registerCompanyUserGoodsListDrawer, { openDrawer: openCompanyUserGoodsListDrawer }] = useDrawer();
const { prefixCls, tableContext, handleDelete, handleSuccess, handlePost, handleBatchDelete } = useBaseTable({
  designScope: 'companyOrder-template',
  tableProps: {
    //表格标题
    title: '企业下单列表',
    //主键字段
    rowKey: 'orderId',
    //列表接口
    listUrl: Api.page,
    //列表查询handler
    handlerName: 'busCompanyOrderListHandler',
    //列定义
    columns,
    //查询表单配置
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      showAdvancedButton: false,
      showExportButton: true,
      exportButtonOptions: {
        code: 'ACTION_companyOrderExport',
      },
    },
    //操作列配置
    actionColumn: {
      width: 130,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
  },
});

const [registerTable, { reload }, { rowSelection, hasSelectedRows }] = tableContext;

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '查看券码',
      onClick: handleLookUserGoods.bind(null, record),
      auth: 'ACTION_companyLookUserGoods',
    },
    {
      label: '退款',
      onClick: handleOrderRefund.bind(null, record),
      ifShow: record.orderStatus == 2 && record.categoryCode != 'vipcard_1' && record.categoryCode != 'vipcard_2',
      auth: 'ACTION_companyOrderRefund',
    },
  ];
}

function handleCreate() {
  openCompanyOrderDrawer(true, {
    isUpdate: false,
  });
}

function handleLookUserGoods(record: Recordable) {
  openCompanyUserGoodsListDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleOrderRefund(record: Recordable) {
  createConfirm({
    iconType: 'info',
    title: '订单退款',
    content: '此订单是否退款？',
    onOk: async () => {
      await companyOrderRefund({ orderId: record.orderId, companyId: record.companyId });
      reload();
    },
    onCancel: async () => {},
  });
}
</script>
